package org.i_plus.controller;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.i_plus.Assistance;
import org.i_plus.model.HobbyBean;
import org.i_plus.model.MemberBean;
import org.i_plus.model.services.MemberService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@WebServlet("/MemberUpdate")
@MultipartConfig
public class MemberUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MemberService service;
	private boolean te15;
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (MemberService) context.getBean("MemberService");		
	} 
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//get資料
		request.setCharacterEncoding("UTF-8");
		HttpSession ses = request.getSession();
		MemberBean temp=(MemberBean)ses.getAttribute("user");
		String temp1 = temp.getAccount();
		//byte[] temp2=(byte[])ses.getAttribute("pass");
		String temp3=request.getParameter("name");
		String temp4=request.getParameter("nickname");
		String temp9=request.getParameter("hight");
		String temp10=request.getParameter("region");
		String temp12=request.getParameter("state");
	    String temp15=request.getParameter("play");
		String code1=request.getParameter("ball");
		String code2=request.getParameter("run");
		String code3=request.getParameter("reading");
		String code4=request.getParameter("swim");
		String code5=request.getParameter("movie");
		String code6=request.getParameter("singing");
		String code7=request.getParameter("bike");
		String code8=request.getParameter("tvgame");
		String x1=request.getParameter("x1");
		String w=request.getParameter("w");
		String y1=request.getParameter("y1");
		String h=request.getParameter("h");
		//if(request.getPart("pic1").)
		InputStream temp5 = request.getPart("image_file").getInputStream();
		String pro=request.getParameter("inp");
		//判別「興趣」欄位值
		boolean bcode1;
		boolean bcode2;
		boolean bcode3;
		boolean bcode4;
		boolean bcode5;
		boolean bcode6;
		boolean bcode7;
		boolean bcode8;
		if(code1==null)
		{
			bcode1=false;
		}
		else{
			bcode1=true;
		}
		if(code2==null)
		{
			bcode2=false;
		}
		else{
			bcode2=true;
		}
		if(code3==null)
		{
			bcode3=false;
		}
		else{
			bcode3=true;
		}
		if(code4==null)
		{
			bcode4=false;
		}
		else{
			bcode4=true;
		}
		if(code5==null)
		{
			bcode5=false;
		}
		else{
			bcode5=true;
		}
		if(code6==null)
		{
			bcode6=false;
		}
		else{
			bcode6=true;
		}
		if(code7==null)
		{
			bcode7=false;
		}
		else{
			bcode7=true;
		}
		if(code8==null)
		{
			bcode8=false;
		}
		else{
			bcode8=true;
		}
		//資料型態轉型
		
		int hi=Integer.parseInt(temp9);
		if(temp15==null || temp15.length()==0)
		{
			te15=false;
		}
		else{
		boolean te15=temp15.contentEquals(temp15);
		this.te15=te15;
		}
//		byte[] pic1 = new byte[(int)request.getPart("pic1").getSize()];
//			temp5.read(pic1);
//		byte[] pic2 = new byte[(int)request.getPart("pic2").getSize()];
//			temp6.read(pic2);
		//塞資料進MemberBean裡面	
		MemberBean bean=(MemberBean)ses.getAttribute("user");
		//MemberBean bean=new MemberBean();
//		bean.setAccount(temp1);
		bean.setName(temp3);
		bean.setNickname(temp4);
		bean.setHight(hi);
		bean.setRegion(temp10);
		bean.setState(temp12);
		bean.setPlay(te15);
		bean.setPop(1);
		if(request.getPart("image_file").getSize()!=0){
		
		byte[] image_file = new byte[(int)request.getPart("image_file").getSize()];
		temp5.read(image_file);
		image_file = Assistance.cropper(image_file, Integer.parseInt(y1), Integer.parseInt(x1), Integer.parseInt(w),Integer.parseInt(h));
 		bean.setPic1(image_file);
 		}
		
 		//塞資料進HobbyBean裡面
     	HobbyBean hbean=new HobbyBean();
     	hbean.setAccount(temp1);
		hbean.setBall(bcode1);
		hbean.setRun(bcode2);
		hbean.setReading(bcode3);
		hbean.setSwim(bcode4);
		hbean.setMovie(bcode5);
		hbean.setSinging(bcode6);
		hbean.setBike(bcode7);
		hbean.setTvgame(bcode8); 
		
		
     	if(pro.equals("update"))
     	{  
     		service.updateMember(bean);
     		service.updateHobby(hbean);
     		ses.setAttribute("userpic", Assistance.photoByteToImgString("image/jpeg", bean.getPic1()));
     		ses.setAttribute("user", bean);
     		 if(bean.getSex().equals("m")){ 
                 request.setAttribute("bg", "男"); 
             }else{ 
                 request.setAttribute("bg", "女"); 
             } 
     		Assistance ass=new Assistance();//new 這個class 
            String blood=ass.howTypeBlood(bean.getBlood());//呼叫方法，判斷血型！ 
            request.setAttribute("blood", blood); 
            int old=ass.howOldAreYou(bean.getBdate());//呼叫方法！這方法是判斷幾歲！ 
            request.setAttribute("old",old); 
            String star=ass.howSignAreYou(bean.getBdate());//呼叫方法！這方法是判斷什麼星座！ 
            request.setAttribute("starSign", star); 
            String hobbys=ass.hobbyTrans(hbean); 
            request.setAttribute("hobby", hobbys);
            request.setAttribute("temppicture1", Assistance.photoByteToImgString("image/jpeg", bean.getPic1()));
    		request.getRequestDispatcher("/index.jsp").
    		forward(request, response);
     	}
     }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
